<template>
  <div class="login">
    <div class="login_box">
      <img class="logo" src="@/assets/common/login-logo.png" alt="" />
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="mobile">
          <el-input prefix-icon="el-icon-user-solid" v-model="form.mobile" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            show-password
            v-model="form.password"
            @keyup.enter.native="tologin"
          />
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" @click="tologin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    // 设置手机号的正则表达式
    const phonenum = (rulse, val, callback) => {
      const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
      if (!reg.test(val)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      form: {
        mobile: '13800000002',
        password: ''
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'change',
            validator: phonenum
          },
          { min: 11, max: 11, message: '请输入正确的手机号', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    tologin () {
      this.$refs.form.validate(async val => {
        if (val) {
          this.loading = true
          try {
            await this.$store.dispatch('user/tologin', this.form)
            this.loading = false
            const req = this.$route.query.result || '/'
            this.$router.push(req)
          } catch {
            this.loading = false
          }
        } else {
          console.log('失败了')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  background: url('~@/assets/common/login.jpg');
  width: 100%;
  height: 100%;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    ::v-deep .el-input__inner {
      height: 100px;
    }
    width: 450px;
    text-align: center;
    .el-button {
      width: 100%;
      border: none;
      background-color: green;
      color: white;
    }
    .logo {
      padding-bottom: 20px;
    }
  }
}
</style>
